home *** CD-ROM | disk | FTP | other *** search
/ Visual Cafe 3 / Visual Cafe 3.ISO / Vcafe / JFC.bin / LookAndFeel.java < prev    next >
Text File  |  1998-06-30  |  10KB  |  277 lines

  1. /*
  2.  * @(#)LookAndFeel.java    1.9 98/01/30
  3.  * 
  4.  * Copyright (c) 1997 Sun Microsystems, Inc. All Rights Reserved.
  5.  * 
  6.  * This software is the confidential and proprietary information of Sun
  7.  * Microsystems, Inc. ("Confidential Information").  You shall not
  8.  * disclose such Confidential Information and shall use it only in
  9.  * accordance with the terms of the license agreement you entered into
  10.  * with Sun.
  11.  * 
  12.  * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF THE
  13.  * SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
  14.  * IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
  15.  * PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR ANY DAMAGES
  16.  * SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR DISTRIBUTING
  17.  * THIS SOFTWARE OR ITS DERIVATIVES.
  18.  * 
  19.  */
  20.  
  21. package com.sun.java.swing;
  22.  
  23. import java.awt.Font;
  24. import java.awt.Color;
  25. import java.awt.SystemColor;
  26.  
  27. import com.sun.java.swing.plaf.basic.*;
  28. import com.sun.java.swing.BorderFactory;
  29. import com.sun.java.swing.JComponent;
  30. import com.sun.java.swing.ImageIcon;
  31. import com.sun.java.swing.UIDefaults;
  32. import com.sun.java.swing.UIManager;
  33. import com.sun.java.swing.border.*;
  34. import com.sun.java.swing.plaf.*;
  35.  
  36. import java.net.URL;
  37. import java.io.*;
  38.  
  39. /**
  40.  * Completely characterizes a look and feel from the point of view
  41.  * of the pluggable look and feel components.  
  42.  * 
  43.  * @version 1.9 01/30/98
  44.  * @author Tom Ball
  45.  * @author Hans Muller
  46.  */
  47. public abstract class LookAndFeel 
  48. {
  49.  
  50.     /**
  51.      * Convenience method for initializing a component's foreground
  52.      * and background color properties with values from the current
  53.      * defaults table.  The properties are only set if the current
  54.      * value is either null or a UIResource.
  55.      * 
  56.      * @param c the target component for installing default color/font properties
  57.      * @param defaultBgName the key for the default background
  58.      * @param defaultFgName the key for the default foreground
  59.      * 
  60.      * @see #installColorsAndFont
  61.      * @see UIManager#getColor
  62.      */
  63.     public static void installColors(JComponent c,
  64.                      String defaultBgName,
  65.                                      String defaultFgName)
  66.     {
  67.         Color bg = c.getBackground();
  68.     if (bg == null || bg instanceof UIResource) {
  69.         c.setBackground(UIManager.getColor(defaultBgName));
  70.     }
  71.  
  72.         Color fg = c.getForeground();
  73.     if (fg == null || fg instanceof UIResource) {
  74.         c.setForeground(UIManager.getColor(defaultFgName));
  75.     } 
  76.     }
  77.  
  78.  
  79.     /**
  80.      * Convenience method for initializing a components foreground
  81.      * background and font properties with values from the current
  82.      * defaults table.  The properties are only set if the current
  83.      * value is either null or a UIResource.
  84.      * 
  85.      * @param c the target component for installing default color/font properties
  86.      * @param defaultBgName the key for the default background
  87.      * @param defaultFgName the key for the default foreground
  88.      * @param defaultFontName the key for the default font
  89.      * 
  90.      * @see #installColors
  91.      * @see UIManager#getColor
  92.      * @see UIManager#getFont
  93.      */
  94.     public static void installColorsAndFont(JComponent c,
  95.                                          String defaultBgName,
  96.                                          String defaultFgName,
  97.                                          String defaultFontName) {
  98.         Font f = c.getFont();
  99.     if (f == null || f instanceof UIResource) {
  100.         c.setFont(UIManager.getFont(defaultFontName));
  101.     }
  102.  
  103.     installColors(c, defaultBgName, defaultFgName);
  104.     }
  105.  
  106.     /**
  107.      * Convenience method for installing a component's default Border 
  108.      * object on the specified component if either the border is 
  109.      * currently null or already an instance of UIResource.
  110.      * @param c the target component for installing default border
  111.      * @param defaultBorderName the key specifying the default border
  112.      */
  113.     public static void installBorder(JComponent c, String defaultBorderName) {
  114.         Border b = c.getBorder();
  115.         if (b == null || b instanceof UIResource) {
  116.             c.setBorder(UIManager.getBorder(defaultBorderName));
  117.         }
  118.     }
  119.  
  120.     /**
  121.      * Convenience method for un-installing a component's default 
  122.      * border on the specified component if the border is 
  123.      * currently an instance of UIResource.
  124.      * @param c the target component for uninstalling default border
  125.      */
  126.     public static void uninstallBorder(JComponent c) {
  127.         if (c.getBorder() instanceof UIResource) {
  128.             c.setBorder(null);
  129.         }
  130.     }
  131.  
  132.     /**
  133.      * Utility method that creates a UIDefaults.LazyValue that creates
  134.      * an ImageIcon UIResource for the specified <code>gifFile</code>
  135.      * filename.
  136.      */
  137.     public static Object makeIcon(final Class baseClass, final String gifFile) {
  138.     return new UIDefaults.LazyValue() {
  139.         public Object createValue(UIDefaults table) {
  140.                 byte[] buffer = null;
  141.                 try {
  142.                     /* Copy resource into a byte array.  This is
  143.                      * necessary because several browsers consider
  144.                      * Class.getResource a security risk because it
  145.                      * can be used to load additional classes.
  146.                      * Class.getResourceAsStream just returns raw
  147.                      * bytes, which we can convert to an image.
  148.                      */
  149.                     InputStream resource = 
  150.                         baseClass.getResourceAsStream(gifFile);
  151.                     if (resource == null) {
  152.                         System.err.println(baseClass.getName() + "/" + 
  153.                                            gifFile + " not found.");
  154.                         return null; 
  155.                     }
  156.                     BufferedInputStream in = 
  157.                         new BufferedInputStream(resource);
  158.                     ByteArrayOutputStream out = 
  159.                         new ByteArrayOutputStream(1024);
  160.                     buffer = new byte[1024];
  161.                     int n;
  162.                     while ((n = in.read(buffer)) > 0) {
  163.                         out.write(buffer, 0, n);
  164.                     }
  165.                     in.close();
  166.                     out.flush();
  167.  
  168.                     buffer = out.toByteArray();
  169.                     if (buffer.length == 0) {
  170.                         System.err.println("warning: " + gifFile + 
  171.                                            " is zero-length");
  172.                         return null;
  173.                     }
  174.                 } catch (IOException ioe) {
  175.                     System.err.println(ioe.toString());
  176.                     return null;
  177.                 }
  178.  
  179.                 return new IconUIResource(new ImageIcon(buffer));
  180.         }
  181.     };
  182.     }
  183.  
  184.     /**
  185.      * Return a short string that identifies this look and feel, e.g.
  186.      * "CDE/Motif".  This string should be appropriate for a menu item.
  187.      * Distinct look and feels should have different names, e.g. 
  188.      * a subclass of MotifLookAndFeel that changes the way a few components
  189.      * are rendered should be called "CDE/Motif My Way"; something
  190.      * that would be useful to a user trying to select a L&F from a list
  191.      * of names.
  192.      */
  193.     public abstract String getName();
  194.  
  195.  
  196.     /**
  197.      * Return a string that identifies this look and feel.  This string 
  198.      * will be used by applications/services that want to recognize
  199.      * well known look and feel implementations.  Presently
  200.      * the well known names are "Motif", "Windows", "Mac", "Organic".  Note 
  201.      * that a derived LookAndFeel that doesn't make any fundamental changes
  202.      * to the look or feel should not override this method.
  203.      */
  204.     public abstract String getID();
  205.  
  206.  
  207.     /** 
  208.      * Return a one line description of this look and feel implementation, 
  209.      * e.g. "The CDE/Motif Look and Feel".   This string is intended for 
  210.      * the user, e.g. in the title of a window or in a ToolTip message.
  211.      */
  212.     public abstract String getDescription();
  213.  
  214.  
  215.     /**
  216.      * If the underlying platform has a "native" look and feel, and this
  217.      * is an implementation of it, return true.  For example a CDE/Motif
  218.      * look and implementation would return true when the underlying 
  219.      * platform was Solaris.
  220.      */
  221.     public abstract boolean isNativeLookAndFeel();
  222.  
  223.  
  224.     /**
  225.      * Return true if the underlying platform supports and or permits
  226.      * this look and feel.  This method returns false if the look 
  227.      * and feel depends on special resources or legal agreements that
  228.      * aren't defined for the current platform.  
  229.      * 
  230.      * @see UIManager#setLookAndFeel
  231.      */
  232.     public abstract boolean isSupportedLookAndFeel();
  233.  
  234.  
  235.     /**
  236.      * UIManager.setLookAndFeel calls this method before the first
  237.      * call (and typically the only call) to getDefaults().  Subclasses
  238.      * should do any one-time setup they need here, rather than 
  239.      * in a static initializer, because look and feel class objects
  240.      * may be loaded just to discover that isSupportedLookAndFeel()
  241.      * returns false.
  242.      *
  243.      * @see #uninitialize
  244.      * @see UIManager#setLookAndFeel
  245.      */
  246.     public void initialize() {
  247.     }
  248.  
  249.  
  250.     /**
  251.      * UIManager.setLookAndFeel calls this method just before we're
  252.      * replaced by a new default look and feel.   Subclasses may 
  253.      * choose to free up some resources here.
  254.      *
  255.      * @see #initialize
  256.      */
  257.     public void uninitialize() {
  258.     }
  259.  
  260.     /**
  261.      * This method is called once by UIManager.setLookAndFeel to create
  262.      * the look and feel specific defaults table.  Other applications,
  263.      * for example an application builder, may also call this method.
  264.      *
  265.      * @see #initialize
  266.      * @see #uninitialize
  267.      * @see UIManager#setLookAndFeel
  268.      */
  269.     public UIDefaults getDefaults() {
  270.         return null;
  271.     }
  272.  
  273.     public String toString() {
  274.     return "[" + getDescription() + " - " + getClass().getName() + "]";
  275.     }
  276. }
  277.